<meta charset="utf-8">
(#) Incorrect support annotation usage

!!! ERROR: Incorrect support annotation usage
   This is an error.

Id
:   `SupportAnnotationUsage`
Summary
:   Incorrect support annotation usage
Severity
:   Error
Category
:   Correctness
Platform
:   Any
Vendor
:   Android Open Source Project
Feedback
:   https://issuetracker.google.com/issues/new?component=192708
Since
:   2.2.0 (September 2016)
Affects
:   Kotlin and Java files
Editing
:   This check runs on the fly in the IDE editor
Implementation
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/AnnotationDetector.kt)
Tests
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/AnnotationDetectorTest.kt)

This lint check makes sure that the support annotations (such as
`@IntDef` and `@ColorInt`) are used correctly. For example, it's an
error to specify an `@IntRange` where the `from` value is higher than
the `to` value.

!!! Tip
   This lint check has an associated quickfix available in the IDE.

(##) Example

Here is an example of lint warnings produced by this check:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
src/test/pkg/Annotation1.kt:14:Error: Use @kotlin.annotation.Target, not
@java.lang.annotation.Target here; these targets will be ignored from
Kotlin and the annotation will not be allowed on any element types from
Java [SupportAnnotationUsage]
@java.lang.annotation.Target(ElementType.PARAMETER) // ERROR 1
                      ------
src/test/pkg/Annotation1.kt:19:Error: Do not use
@java.lang.annotation.Target here; it will cause the annotation to not
be allowed on any element types from Java [SupportAnnotationUsage]
@java.lang.annotation.Target(ElementType.PARAMETER) // ERROR 2
                      ------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here is the source file referenced above:

`src/test/pkg/Annotation1.kt`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers
package test.pkg

import java.lang.annotation.ElementType

// Correct: can be used on any element from Kotlin and Java
annotation class Annotation1() // OK 1

// Correct: can be used on parameters only both in Kotlin and Java
@Target(AnnotationTarget.VALUE_PARAMETER) // OK 2
annotation class Annotation2()

// Incorrect: can be used on any element (not just parameters) in
// Kotlin, and cannot be used on any elements (including parameters) in Java
@java.lang.annotation.Target(ElementType.PARAMETER) // ERROR 1
annotation class Annotation3()

// Incorrect: works fine from Kotlin (can be used only on parameters),
// but cannot be used on any elements from Java
@java.lang.annotation.Target(ElementType.PARAMETER) // ERROR 2
@Target(AnnotationTarget.VALUE_PARAMETER)
annotation class Annotation4()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can also visit the
[source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/AnnotationDetectorTest.kt)
for the unit tests for this check to see additional scenarios.

The above example was automatically extracted from the first unit test
found for this lint check, `AnnotationDetector.testAnnotationTarget`.
To report a problem with this extracted sample, visit
https://issuetracker.google.com/issues/new?component=192708.

(##) Suppressing

You can suppress false positives using one of the following mechanisms:

* Using a suppression annotation like this on the enclosing
  element:

  ```kt
  // Kotlin
  @Suppress("SupportAnnotationUsage")
  fun method() {
     problematicStatement()
  }
  ```

  or

  ```java
  // Java
  @SuppressWarnings("SupportAnnotationUsage")
  void method() {
     problematicStatement();
  }
  ```

* Using a suppression comment like this on the line above:

  ```kt
  //noinspection SupportAnnotationUsage
  problematicStatement()
  ```

* Using a special `lint.xml` file in the source tree which turns off
  the check in that folder and any sub folder. A simple file might look
  like this:
  ```xml
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;lint&gt;
      &lt;issue id="SupportAnnotationUsage" severity="ignore" /&gt;
  &lt;/lint&gt;
  ```
  Instead of `ignore` you can also change the severity here, for
  example from `error` to `warning`. You can find additional
  documentation on how to filter issues by path, regular expression and
  so on
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html).

* In Gradle projects, using the DSL syntax to configure lint. For
  example, you can use something like
  ```gradle
  lintOptions {
      disable 'SupportAnnotationUsage'
  }
  ```
  In Android projects this should be nested inside an `android { }`
  block.

* For manual invocations of `lint`, using the `--ignore` flag:
  ```
  $ lint --ignore SupportAnnotationUsage ...`
  ```

* Last, but not least, using baselines, as discussed
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).

<!-- Markdeep: --><style class="fallback">body{visibility:hidden;white-space:pre;font-family:monospace}</style><script src="markdeep.min.js" charset="utf-8"></script><script src="https://morgan3d.github.io/markdeep/latest/markdeep.min.js" charset="utf-8"></script><script>window.alreadyProcessedMarkdeep||(document.body.style.visibility="visible")</script>